#include <stdlib.h>
#include <strings.h>
#include <stdio.h>
#include "MacBytes.hpp"

using namespace BFT::messages;

MacBytes::MacBytes()
{	
	bytes.reset((char*)malloc(size()),free);
	bzero((void*)bytes.get(),size());
}

MacBytes::MacBytes(boost::shared_ptr<char> _bytes, int _bytes_size)
{
	if ((unsigned int)_bytes_size != size())
	{
		fprintf(stderr,"trying to create a mac from a byte array of wrong size. \
				expected size: %d, actual size: %d\n",size(),_bytes_size);
		exit(90);
	}
	bytes = _bytes;
}

MacBytes::MacBytes(boost::shared_ptr<char> _bytes)
{
	/*
	if ((unsigned int)_bytes_size != size())
	{
		fprintf(stderr,"trying to create a mac from a byte array of wrong size. \
				expected size: %d, actual size: %d\n",size(),_bytes_size);
		exit(90);
	}
	*/
	bytes = _bytes;
}

bool MacBytes::equals(boost::shared_ptr<MacBytes>_b)
{
	bool equal = true;
	boost::shared_ptr<char> other_bytes = _b.get()->getBytes();
	
	for(unsigned int i = 0; i<size() && equal ; i++)
		equal = bytes.get()[i] == other_bytes.get()[i];
	return equal;
}

void MacBytes::printInfo()
{
	for (unsigned int i=0; i< size(); i++)
		fprintf(stdout,"|%02X",(unsigned char)bytes.get()[i]);

	fprintf(stdout,"\n");
}

uint32_t MacBytes::size() 
{
	return MAC_SIZE;
}
